package com.example.chessmigame;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class ListDetailsActivity extends AppCompatActivity{
    private EditText title;
    private EditText context;
    private int note_id=0;
    private String get_title;
    private String get_context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_details);

        title=(EditText) findViewById(R.id.title_detail);
        context=(EditText) findViewById(R.id.context_detail);

        //接收listView中点击item传来的note_id,
        Intent intent=getIntent();
        note_id=intent.getIntExtra("note_id",0);
        NoteManager noteManager=new NoteManager( this);
        Note note = noteManager.getNoteById(note_id);

        title.setText(String.valueOf(note.title));
        context.setText(String.valueOf(note.context));
    }

    public void finshchange(View v){
        get_title=title.getText().toString().trim();
        get_context=context.getText().toString().trim();
        if(TextUtils.isEmpty(get_title)||TextUtils.isEmpty(get_context)){
            Toast.makeText(this,"修改内容不能为空",Toast.LENGTH_SHORT).show();
        }else{
            Note note=new Note();
            note.note_id=note_id;
            note.title=get_title;
            note.context=get_context;
            NoteManager noteManager=new NoteManager(ListDetailsActivity.this);
            noteManager.update(note);

            Toast.makeText(this,"修改成功",Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(ListDetailsActivity.this,ListActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
    }

    public void detailtolist(View view) {
        Intent intent = new Intent(ListDetailsActivity.this, ListActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }
}
